package com.guchenbo.spark.sql

import org.apache.commons.lang3.StringUtils

import scala.util.matching.Regex

object RegexDemo {
  def main(args: Array[String]): Unit = {
    var insertSql =
      """
        |with ds_t3126 as ( SELECT a.*,b.name as nameb
        |      FROM model_manager.model_info a
        |               LEFT JOIN model_manager.model_form_template b ON a.form_template_uuid = b.uuid )insert overwrite table MONITOR_TYPE_134_Table_0 select * from (( select name as v1,nameb as v2 from ds_t3126 ) as field_0)
        |""".stripMargin


    val viewName = StringUtils.substringAfter(StringUtils.substringBefore(insertSql, "as"), "with")
    val idx = StringUtils.indexOf(insertSql, "insert overwrite table")
    val insSql = insertSql.substring(idx)
    var selSql = insertSql.substring(0, idx)
    selSql = StringUtils.substringBefore(StringUtils.substringAfter(selSql, "("), ")")
    println(viewName)
    println(insSql)
    println(selSql)
    //
    //
    //    val pattern: Regex = "with (.*) as \\((.*)\\)+(.*)".r
    //    var i=1
    //    for (patternMatch <- pattern.findAllMatchIn(insertSql)) {
    //
    //      println(s"find ${patternMatch.group(1)}")
    //      println(s"find ${patternMatch.group(2)}")
    //      println(s"find ${patternMatch.group(3)}")
    //    }
  }
}
